package com.magicode.screen.api;

import java.util.Locale;

import com.loopj.android.http.SyncHttpClient;
import com.magicode.screen.AppContext;
//import com.magicode.screen.util.Md5TimeSignUtil;
import com.magicode.screen.util.TLog;

import org.apache.http.client.params.ClientPNames;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class ApiHttpClient {
    public final boolean sslFlag = true;
    private static String HOST = "app.media-plus.cn";
    private static String API_URL = "http://app.media-plus.cn/%s";
    public static final String DELETE = "DELETE";
    public static final String GET = "GET";
    public static final String POST = "POST";
    public static final String PUT = "PUT";
    public static AsyncHttpClient client;
    public static SyncHttpClient syncHttpClient;


//    public static AsyncSslClient sslClient;
//    public static AsyncSslClient sslClient;

    public ApiHttpClient() {


    }

    public static SyncHttpClient getSyncHttpClient() {
        return syncHttpClient;
    }
    public static AsyncHttpClient getHttpClient() {
        return client;
    }

    public static void cancelAll(Context context) {
        client.cancelRequests(context, true);
    }

    public static void clearUserCookies(Context context) {
        // (new HttpClientCookieStore(context)).a();
    }

    public static void delete(String partUrl, AsyncHttpResponseHandler handler) {
        client.delete(getAbsoluteApiUrl(partUrl), handler);
        log(new StringBuilder("DELETE ").append(partUrl).toString());
    }

    public static void get(String partUrl, AsyncHttpResponseHandler handler) {

//        Md5TimeSignUtil mtsu = new Md5TimeSignUtil();
//        Md5TimeSignUtil.Md5SignClass msc = mtsu.getMd5Sign(partUrl,null);
        if(partUrl.trim().indexOf("http") == -1 || partUrl.trim().indexOf("http") > 0 ){
            partUrl =  getAbsoluteApiUrl(partUrl);
        }

   //     client.get(partUrl, msc.params, handler);
        client.get(getAbsoluteApiUrl(partUrl), handler);
        log(new StringBuilder("GET ").append(partUrl).toString());
    }

    public static void getSync(String partUrl) {
//        syncHttpClient.get(getAbsoluteApiUrl(partUrl));
        log(new StringBuilder("GET ").append(partUrl).toString());
    }
    public static void get(String partUrl, RequestParams params,
            AsyncHttpResponseHandler handler) {
//        Md5TimeSignUtil mtsu = new Md5TimeSignUtil();
//        Md5TimeSignUtil.Md5SignClass msc = mtsu.getMd5Sign(partUrl,params);
        if(partUrl.trim().indexOf("http") == -1 || partUrl.trim().indexOf("http") > 0 ){
            partUrl =  getAbsoluteApiUrl(partUrl);
        }

//        client.get(partUrl, msc.params, handler);
        client.get(getAbsoluteApiUrl(partUrl), params, handler);
        log(new StringBuilder("GET ").append(partUrl).append("&")
                .append(params).toString());
    }

    public static String getAbsoluteApiUrl(String partUrl) {
        String url = String.format(getApiUrl(), partUrl);
        Log.d("BASE_CLIENT", "request:" + url);
        return url;
    }

    public static String getApiUrl() {
        return API_URL;
    }

    public static void getDirect(String url, AsyncHttpResponseHandler handler) {

//        Md5TimeSignUtil mtsu = new Md5TimeSignUtil();
//        Md5TimeSignUtil.Md5SignClass msc = mtsu.getMd5Sign(url,null);
        if(url.trim().indexOf("http") == -1 || url.trim().indexOf("http") > 0 ){
            url =  getAbsoluteApiUrl(url);
        }

//        client.post(url, msc.params, handler);
        client.get(url, handler);
        log(new StringBuilder("GET ").append(url).toString());
    }

    public static void log(String log) {
        Log.d("BaseApi", log);
        TLog.log("Test", log);
    }

    public static void post(String partUrl, AsyncHttpResponseHandler handler) {

//        Md5TimeSignUtil mtsu = new Md5TimeSignUtil();
//        Md5TimeSignUtil.Md5SignClass msc = mtsu.getMd5Sign(partUrl,null);
        if(partUrl.trim().indexOf("http") == -1 || partUrl.trim().indexOf("http") > 0 ){
            partUrl =  getAbsoluteApiUrl(partUrl);
        }

//        client.post(partUrl, msc.params, handler);
        client.post(getAbsoluteApiUrl(partUrl), handler);
        log(new StringBuilder("POST ").append(partUrl).toString());
    }

    public static void post(String partUrl, RequestParams params,
            AsyncHttpResponseHandler handler) {
        String url = null;
        if(partUrl == null){
            return;
        }

//        Md5TimeSignUtil mtsu = new Md5TimeSignUtil();
//        Md5TimeSignUtil.Md5SignClass msc = mtsu.getMd5Sign(partUrl,params);
        if(partUrl.trim().indexOf("http") == -1 || partUrl.trim().indexOf("http") > 0 ){
            url =  getAbsoluteApiUrl(partUrl);
        }

//        client.post(url, msc.params, handler);
        client.post(url,params,handler);

        log(new StringBuilder("POST ").append(partUrl).append("&")
                .append(params).toString());
    }


    public static void postDirect(String url, RequestParams params,
            AsyncHttpResponseHandler handler) {

//        Md5TimeSignUtil mtsu = new Md5TimeSignUtil();
//        Md5TimeSignUtil.Md5SignClass msc = mtsu.getMd5Sign(url,params);
//        if(url.trim().indexOf("http") == -1 || url.trim().indexOf("http") > 0 ){
//            url =  getAbsoluteApiUrl(msc.url);
//        }
//        else{
//            url =  msc.url;
//        }

//        client.post(url, msc.params, handler);
        client.post(url, params, handler);
        log(new StringBuilder("POST ").append(url).append("&").append(params)
                .toString());
    }

    public static void put(String partUrl, AsyncHttpResponseHandler handler) {

//        Md5TimeSignUtil mtsu = new Md5TimeSignUtil();
//        Md5TimeSignUtil.Md5SignClass msc = mtsu.getMd5Sign(partUrl,null);
        if(partUrl.trim().indexOf("http") == -1 || partUrl.trim().indexOf("http") > 0 ){
            partUrl =  getAbsoluteApiUrl(partUrl);
        }

//        client.put(partUrl, msc.params, handler);
        client.put(getAbsoluteApiUrl(partUrl), handler);
        log(new StringBuilder("PUT ").append(partUrl).toString());
    }

    public static void put(String partUrl, RequestParams params,
            AsyncHttpResponseHandler handler) {
//        Md5TimeSignUtil mtsu = new Md5TimeSignUtil();
//        Md5TimeSignUtil.Md5SignClass msc = mtsu.getMd5Sign(partUrl,null);
//        if(partUrl.trim().indexOf("http") == -1 || partUrl.trim().indexOf("http") > 0 ){
//            partUrl =  getAbsoluteApiUrl(msc.url);
//        }
//        else{
//            partUrl =  msc.url;
//        }
//
//        client.put(partUrl, msc.params, handler);
        client.put(getAbsoluteApiUrl(partUrl), params, handler);
        log(new StringBuilder("PUT ").append(partUrl).append("&")
                .append(params).toString());
    }

    public static void setApiUrl(String apiUrl) {
        API_URL = apiUrl;
    }

    public static void setHttpClient(AsyncHttpClient c) {
        client = c;
        client.addHeader("Accept-Language", Locale.getDefault().toString());
        client.addHeader("Host", HOST);
        client.addHeader("Connection", "Keep-Alive");
        client.getHttpClient().getParams()
                .setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);

        setUserAgent(ApiClientHelper.getUserAgent(AppContext.getInstance()));
    }

    public static void setUserAgent(String userAgent) {
        client.setUserAgent(userAgent);
    }

    public static void setCookie(String cookie) {
        client.addHeader("Cookie", cookie);
    }

    private static String appCookie;

    public static void cleanCookie() {
        appCookie = "";
    }

    public static String getCookie(AppContext appContext) {
        if (appCookie == null || appCookie == "") {
            appCookie = appContext.getProperty("cookie");
        }
        return appCookie;
    }

    public static String getHOST() {
        return HOST;
    }

    public static void setHOST(String HOST) {
        ApiHttpClient.HOST = HOST;
    }


}
